/**
 * File Name   : UpdateDao.java
 * Description : 
 */
package feng.process.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.log4j.Logger;

import com.ibatis.sqlmap.client.SqlMapClient;

import feng.process.dao.bean.ParamTaskUserRefBean;

/**
 * Ibatis方式更新使用Dao：支持update、delete、insert等操作
 * @author 冯昌硕
 * @create 2012-10-25 上午12:44:24
 */
public class UpdateDao {
    
    private static final Logger log = Logger.getLogger(QueryDao.class);

    private SqlMapClient sqlMapClient = SQLMapClientFactory.getSqlMapClient();

    public Object insert(String sqlId) {
        Object result = null;
        try {
            result = sqlMapClient.insert(sqlId);
        } catch (SQLException e) {
            log.error("[insert(String)]SQL:["+sqlId+"] insert异常：" + e.toString());
            e.printStackTrace();
        }
        
        return result;
    }
    
    /**
     * 执行指定的SQL实现记录添加操作
     * @param sqlId 要执行的SQLID
     * @param param SQL执行参数
     * @return 执行结果
     * @create 2012-10-29 下午11:44:14
     */
    public Object insert(String sqlId, Object param) {
        Object result = null;
        try {
            result = sqlMapClient.insert(sqlId, param);
        } catch (SQLException e) {
            log.error("[insert(String,Object)]SQL:["+sqlId+"] insert异常：" + e.toString());
            e.printStackTrace();
        }
        
        return result;
    }
    
    /**
     * 更新记录
     * @param sqlId 要执行的SQLID
     * @param paramObj SQL执行参数
     * @return 执行结果
     * @create 2012-10-29 下午11:46:42
     */
    public int update(String sqlId, Object paramObj) throws SQLException {
        int updRs = -1;
        try {
            updRs = sqlMapClient.update(sqlId, paramObj);
        } catch (SQLException e) {
            log.error("[update(String,Object)]SQL:["+sqlId+"] update异常：" + e.toString());
            e.printStackTrace();
            throw e;
        }
        return updRs;
    }
    
    /**
     * 执行删除操作
     * @param sqlId 删除SQL语句
     * @param paramObj SQL参数
     * @return 删除影响行数
     * @throws SQLException
     * @create 2012-11-7 上午12:16:50
     */
    public int delete(String sqlId, Object paramObj) throws SQLException {
        int updRs = -1;
        try {
            updRs = sqlMapClient.delete(sqlId, paramObj);
        } catch (SQLException e) {
            log.error("[delete(String,Object)]SQL:["+sqlId+"] update异常：" + e.toString());
            e.printStackTrace();
            throw e;
        }
        return updRs;
    }
    
    /**
     * 功能：开启事务
     * @throws SQLException
     * @create 2012-10-28 下午8:20:56
     */
    public void startTransaction() throws SQLException {
        sqlMapClient.startTransaction();
    }
    
    /**
     * 功能：提交事务
     * @throws SQLException
     * @create 2012-10-28 下午8:22:06
     */
    public void commitTransaction() throws SQLException {
        sqlMapClient.commitTransaction();
    }
    
    /**
     * 功能：结束事务，包括回滚
     * @throws SQLException
     * @create 2012-10-28 下午8:23:50
     */
    public void endTransaction() throws SQLException {
        sqlMapClient.endTransaction();
    }
    
    /**
     * 开启批处理
     * @throws SQLException
     * @create 2012-10-29 下午8:47:54
     */
    public void startBatch() throws SQLException {
        sqlMapClient.startBatch();
    }
    
    public int executeBatch() throws SQLException {
        return sqlMapClient.executeBatch();
    }
    
    /**
     * 批量操作插入待办人员与环节的关系表
     * @param userLst 环节待办人员
     * @param tkid 环节ID
     * @return 批量处理结果
     * @throws SQLException
     * @create 2012-10-29 下午9:02:56
     */
    public int batchExecuteWaitUser(List<?> userLst, long tkid) throws SQLException {
        if((null == userLst) || (userLst.size() <= 0)) {
            log.info("查询的用户列表为空，不作待办人员添加处理");
            return -1;
        }
        
        startBatch();
        
        String userId = null;
        ParamTaskUserRefBean taskUserBean = null;
        int userLstSize = userLst.size();
        for(int i = 0; i < userLstSize; i++) {
            userId = (String) userLst.get(i);
            taskUserBean = new ParamTaskUserRefBean();
            taskUserBean.setTkid(tkid);
            taskUserBean.setWaitUserId(userId);
            insert("process_task_user_ref.insTaskWaitUsers", taskUserBean);
        }
        
        int batchExeRs = executeBatch();
        return batchExeRs;
    }
}
